package com.yjhb.android.sbz.tool;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jojo.android.zxlib.tool.ZxDeviceTool;
import com.jojo.android.zxlib.tool.ZxEncryptTool;
import com.jojo.android.zxlib.tool.ZxHttpTool;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.toomee.mengplus.common.widget.TooMeeImageLoader;
import com.toomee.mengplus.manager.TooMeeManager;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.manager.OAIDManager;

/**
 * 创建者 zx
 * 创建时间 2020/9/7 10:41
 */
public class JXWTool implements TooMeeImageLoader {
    private static JXWTool dyTool;
    private Context context;
    private final String mid = "1578";
    private final String token = "1730c7a68a6ae02c4262a4e9544533e3";
    private String utoken;

    public static void init(Context context) {
        dyTool = new JXWTool(context);
    }

    private JXWTool(Context context) {
        this.context = context;

    }

    public static JXWTool getInstance() {
        if (dyTool == null) throw new RuntimeException("请初始化JXWTool");
        return dyTool;
    }


    public void startList() {
        if (Tool.checkLogin(context)) {
            TooMeeManager.init(context, mid, App.userInfo.getId(), token, this);
            TooMeeManager.start(context);

        }
    }

    public void startDetailPage(String advert_id) {
        if (Tool.checkLogin(context)) {
            if (TextUtils.isEmpty(utoken)) {
                reg(advert_id);
            } else {
                TooMeeManager.init(context, mid, App.userInfo.getId(), token, this);
                String url = "http://task.juxiangwan.com/games/details/" + advert_id + "?utoken=" + utoken + "&mid=" + mid + "&from=sdk";
                TooMeeManager.start(context, url);
            }
        }
    }

    private void reg(String advert_id) {
        StringBuilder stringBuilder = new StringBuilder("http://api.juxiangwan.com/?act=fast_reg&act_type=1");
        stringBuilder.append("&mid=" + mid);
        stringBuilder.append("&resource_id=" + App.userInfo.getId());
        stringBuilder.append("&sign=" + sign());
        if (Build.VERSION.SDK_INT < 29) {
            stringBuilder.append("&device_code=" + ZxDeviceTool.getIMEI(context));
        }
        stringBuilder.append("&oaid=" + OAIDManager.getInstance().getOaid());
        stringBuilder.append("&sdkversion=" + Build.VERSION.SDK_INT);
        stringBuilder.append("&from=h5android&version=2.0");
        ZxHttpTool.Get(stringBuilder.toString(), new ZxHttpTool.OnCallback() {
            @Override
            public void onResponse(String response) {
                if (TextUtils.isEmpty(response)) return;
                String[] strings = response.split("&");
                if (strings == null) return;
                for (String s : strings
                ) {
                    if (s.contains("utoken")) {
                        utoken = s.replace("utoken=", "");
                        startDetailPage(advert_id);
                        return;
                    }
                }

            }

            @Override
            public void onErr(int code, String err) {
                ZxLogTool.e("服务器请求失败", err);
            }
        });
    }

    private String sign() {
        return ZxEncryptTool.encryptMD5ToString(mid + App.userInfo.getId() + token).toLowerCase();
    }

    @Override
    public void loadImage(String path, ImageView imageView) {
        Glide.with(context).load(path).into(imageView);
    }

    @Override
    public void loadImage(int imageDrawable, ImageView imageView, int errorView) {
        Glide.with(context).load(imageDrawable).into(imageView);
    }
}
